package com.tj.bo.trade.server.service.rule;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.tj.bo.trade.client.enums.OrderStatusEnum;
import com.tj.bo.trade.client.enums.TradeTypeEnum;
import com.tj.bo.trade.server.entity.BinaryTradeEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 白银规则1: 前一天的净利润大于0，小于2倍件均
 * 白银规则2: 前一天的胜率≥55%
 * Author: Amarendar
 * Date: 2020/8/25 下午3:11
 */
@Service
public class TradeSilverRule1 extends BaseTradeRule {

    private static final String RULE_NAME = "前一天的净利润大于0，小于2倍件均";

    private static final Integer DEFAULT_PRIORITY = 4;

    @Override
    public void checkRouterRule(RouterContext routerContext, List<RuleResultVO> ruleResultVOList) {
        Date endDate = DateUtil.getPatternDate(new Date(), "yyyy-MM-dd");
        Date startDate = DateUtil.addDate(endDate, -1);

        List<BinaryTradeEntity> binaryTradeEntityList = binaryTradeDao.selectsBinaryTradeListByTime(routerContext.getUserCode(), routerContext.getProductCode(), OrderStatusEnum.CLOSING.getCode(), TradeTypeEnum.REAL_TRADE.getCode(), startDate, endDate);
        if (CollectionUtils.isEmpty(binaryTradeEntityList) || binaryTradeEntityList.size() < redisConfigUtil.getChangeOddsUserTradeNumbers()) {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("订单数过少"));
            return;
        }

        int orderNum = 0; //订单数
        int winOrderNum = 0; //胜利数
        double yesterdayTotalAmount = 0; //累计下单金额
        double yesterdayTotalProfit = 0; //累计盈利

        for (BinaryTradeEntity binaryTradeEntity : binaryTradeEntityList) {
            if (binaryTradeEntity.getAmount().compareTo(binaryTradeEntity.getProfitAmount()) == 0) {
                continue;
            }
            yesterdayTotalAmount += binaryTradeEntity.getAmount().doubleValue();
            yesterdayTotalProfit += (binaryTradeEntity.getProfitAmount().doubleValue() - binaryTradeEntity.getAmount().doubleValue());
            orderNum++;

            if (binaryTradeEntity.getProfitAmount().compareTo(binaryTradeEntity.getAmount()) > 0) {
                winOrderNum++;
            }
        }

        if (yesterdayTotalProfit > 0 && yesterdayTotalProfit < (yesterdayTotalAmount / orderNum * 2)) {
            RuleResultVO ruleResultVO = new RuleResultVO(routerContext.getProductCode(), RULE_NAME);
            ruleResultVO.setPriority(DEFAULT_PRIORITY);
            ruleResultVO.setMode(0);
            ruleResultVO.setChangeValue(BigDecimal.valueOf(-0.02));
            ruleResultVO.setMessage("前一天的净利润大于0,小于2倍件均");
            ruleResultVOList.add(ruleResultVO);
        } else if ((double) winOrderNum / orderNum >= 0.55) {
            RuleResultVO ruleResultVO = new RuleResultVO(routerContext.getProductCode(), "前一天的胜率≥55%");
            ruleResultVO.setPriority(DEFAULT_PRIORITY);
            ruleResultVO.setMode(0);
            ruleResultVO.setChangeValue(BigDecimal.valueOf(-0.02));
            ruleResultVO.setMessage("前一天的胜率≥55%");
            ruleResultVOList.add(ruleResultVO);
        } else {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("前一天的净利润大于0,小于2倍件均-条件不满足"));
        }

    }
}
